from rest_framework import serializers
from areas.models import Area


class AreaSerializer(serializers.ModelSerializer):
    class Meta:
        model = Area
        fields = ('id', 'name')


# beijing_area.subs = [area_object, area_object, area_object, .... ]
class SubAreaSerializer(serializers.ModelSerializer):
    subs = AreaSerializer(many=True, read_only=True)    # 关联序列化器来拿到具体的数据　id 和 name

    class Meta:
        model = Area
        fields = ('id', 'name', 'subs')   # 这里拿到的subs是一个一个的对象集合，我们需要的是字典类型

# {
#     "id": xxx
#     'name': 'bj',
#     'subs': [
#         {
#             "id": "110101",
#             "name": "东城区"
#         },
#         {
#             "id": "110102",
#             "name": "西城区"
#         }
#     ]
# }
